package com.qst.service;

import com.qst.dao.MeetingDao;
import com.qst.dao.MeetingRoomDao;
import com.qst.pojo.Meeting;
import com.qst.pojo.MeetingParticipants;
import com.qst.pojo.MeetingRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author Hsing
 * @Date 2021/6/22
 */
@Service
public class MeetingRoomService {
    @Autowired
    private MeetingRoomDao meetingRoomDao;
    @Autowired
    private MeetingDao meetingDao;

    public List<MeetingRoom> getAllMeetingRoom() {
        return meetingRoomDao.searchMeetingRooms();
    }

    public List<Meeting> getMeetingHistory(int roomId) {
        return meetingRoomDao.searchMeetingHistory(roomId);
    }

    public String searchMeetingRoomName(int roomId) {
        return meetingRoomDao.searchMeetingRoomNameById(roomId);
    }

    public List<MeetingRoom> getLeisureMeetingRoom(int peopleNum, String date, String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(date + " " + start + ":00");
        Date endTime = sdf.parse(date + " " + end + ":00");
        List<MeetingRoom> meetingRoomList = meetingRoomDao.searchMeetingRoomByPeopleNum(peopleNum);
        for (int i = 0; i < meetingRoomList.size(); i++) {
            List<Meeting> meetingList = meetingDao.findMeetingByMeetingRoom(meetingRoomList.get(i).getRoomID());
            for (Meeting meeting : meetingList) {
                if ((startTime.compareTo(meeting.getStarttime()) <= 0 && endTime.compareTo(meeting.getStarttime()) > 0) || (startTime.compareTo(meeting.getStarttime()) >= 0 && startTime.compareTo(meeting.getEndtime()) < 0)) {
                    meetingRoomList.remove(i);
                    i--;
                    break;
                }
            }

        }
        return meetingRoomList;
    }

    public void addMeeting(String meetingname, String numberofparticipants, Integer reservationistid, String time, String starttime, String endtime, String roomid, String description, String mps) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(time + " " + starttime + ":00");
        Date endTime = sdf.parse(time + " " + endtime + ":00");
        Meeting meeting = new Meeting();
        meeting.setMeetingname(meetingname);
        MeetingRoom meetingRoom = new MeetingRoom();
        meetingRoom.setRoomID(Integer.valueOf(roomid));
        meeting.setMeetingRoom(meetingRoom);
        meeting.setReservationistid(reservationistid);
        meeting.setNumberofparticipants(Integer.valueOf(numberofparticipants));
        meeting.setStarttime(startTime);
        meeting.setEndtime(endTime);
        meeting.setReservationtime(new Date());
        meeting.setDescription(description);
        meetingDao.saveMeeting(meeting);
        String[] mpsList = mps.split(",");
        MeetingParticipants meetingParticipants = new MeetingParticipants();
        meetingParticipants.setMeetingid(meeting.getMeetingid());
        for (int i = 0; i < mpsList.length; i++) {

            meetingParticipants.setEmployeeid(Integer.valueOf(mpsList[i]));
            System.out.println(meetingParticipants);
            meetingDao.saveMeetingPeople(meetingParticipants);
        }

    }
}
